package com.example.teamwork.Controller;

import com.example.teamwork.Shrio.Bean.ResponseBean;
import com.example.teamwork.informationGet.PageVO;
import com.example.teamwork.informationGet.VisitorVO;
import com.example.teamwork.mapper.VisitorMapper;
import com.example.teamwork.service.VisitorService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.teamwork.redis.RedisUtil;

/**
 * @author  SongPeng
 * @date  2021/11/11 20:38
 * @version 1.0
 * 访客管理模块
 */


@RestController
@RequestMapping("/visitor")
public class VisitorController {
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","访客管理模块");
    }
    @Autowired
    private VisitorService visitorService;
    @Autowired
    private VisitorMapper visitorMapper;
    @Autowired
    RedisUtil redisUtil;
    public String report_key = "report";

    private static final Logger logger = LogManager.getLogger(VisitorController.class);
    /**
     * 添加访客记录
     * @param visitorVO
     * @return
     */
    @RequiresPermissions({"regist"})
    @PostMapping("/regist")
    public ResponseBean add(@RequestBody @Validated VisitorVO visitorVO) {
        logger.info("添加预约访客记录");
        visitorService.add(visitorVO);
        return ResponseBean.success();
    }
    /**
     *
     * 查询访客记录列表
     */
    @RequiresPermissions("visitor:select")
    @GetMapping("/listData")
    public ResponseBean  listData(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, VisitorVO visitorVO, @RequestParam  Date startTime, @RequestParam  Date endTime){
        PageVO<VisitorVO> userList=visitorService.findVisitorList(pageNum,pageSize,visitorVO,startTime,endTime);
        return ResponseBean.success(userList);
    }
    /**
     * 访客登记报表
     * @return
     */
    @PostMapping("/VisitorReport")
    public ResponseBean loginReport(){
        if(redisUtil.hasKey(report_key)){
            return ResponseBean.success(redisUtil.get(report_key));
        }
        else{
            logger.info("访客登记报表");
            List<Map<String,Object>> enterList= visitorService.VisitorReport();
            Map<String,Object> map=new HashMap<>();
            List<Map<String,Object>> leaveList= visitorMapper.LeaveReport();
            map.put("enter",enterList);
            map.put("leave",leaveList);
            redisUtil.set(report_key,map,86400);
            return ResponseBean.success(map);
        }
    }
    /**
     * 设置访客离开
     * @param visitorId
     * @return
     */
    @RequiresPermissions({"visitor:update"})
    @PutMapping("/set/{visitorId}")
    public ResponseBean edit(@PathVariable Long visitorId) throws InterruptedException {
        logger.info("设置访客离开");
        //此时登记报表数据可能更新，保证一致性(延时双删
        redisUtil.del(report_key);
        visitorService.set(visitorId);
        Thread.sleep(1000);
        redisUtil.del(report_key);
        return ResponseBean.success();
    }
    /**
     * 删除访客
     * */
    @RequiresPermissions("visitor:delete")
    @DeleteMapping("/delete/{visitorId}")
     public ResponseBean delete(@PathVariable Long visitorId) throws InterruptedException {
        logger.info("删除访客");
        //此时登记报表数据可能更新，保证一致性(延时双删
        redisUtil.del(report_key);
        visitorService.delete(visitorId);
        Thread.sleep(1000);
        redisUtil.del(report_key);
        return ResponseBean.success();
    }
    /**
     * 访客详细
     * */
    @RequiresPermissions("visitor:detail")
    @GetMapping("/detail/{visitorId}")
    public ResponseBean detail(@PathVariable Long visitorId){
        logger.info("访客详细");
        VisitorVO visitorVO=visitorService.detail(visitorId);
        return ResponseBean.success(visitorVO);
    }


}